package com.jack.online.study.controller.vod;


import com.jack.online.study.common.base.result.R;
import com.jack.online.study.common.base.result.ResultCodeEnum;
import com.jack.online.study.common.exception.GuliException;
import com.jack.online.study.common.util.ExceptionUtils;
import com.jack.online.study.service.vod.MediaService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@CrossOrigin //跨域
@RestController
@RequestMapping("/admin/vod/media")
@Slf4j
public class MediaController {

    @Resource
    private MediaService mediaService;

    @PostMapping("upload")
    public R uploadVideo(@RequestParam("file") MultipartFile file) {
        try {
            String originalFilename = file.getOriginalFilename();
            String videoId = mediaService.uploadVideo(file, originalFilename);
            return R.ok().message("视频上传成功").data("videoId", videoId);
        } catch (Exception e) {
            log.error(ExceptionUtils.getMessage(e));
            throw new GuliException(ResultCodeEnum.VIDEO_UPLOAD_TOMCAT_ERROR);
        }

    }

    @GetMapping("play/video/{videoId}")
    public ResponseEntity<Object> playVideo(@PathVariable("videoId") Long videoId){
        return mediaService.playVideo(videoId);
    }

    /**
     * 根据id删除视频
     */
    @DeleteMapping("remove/{vodId}")
    public R removeVideo(@PathVariable String vodId){

        try {
            mediaService.removeVideo(vodId);
            return R.ok().message("视频删除成功");
        } catch (Exception e) {
            log.error(ExceptionUtils.getMessage(e));
            throw new GuliException(ResultCodeEnum.VIDEO_DELETE_ALIYUN_ERROR);
        }
    }

    /**
     * 批量删除视频
     * @param videoIdList 视频id列表
     */
    @DeleteMapping("remove")
    public R removeVideoByIdList(@RequestBody List<String> videoIdList){

        try {
            mediaService.removeVideoByIdList(videoIdList);
            return  R.ok().message("视频删除成功");
        } catch (Exception e) {
            log.error(ExceptionUtils.getMessage(e));
            throw new GuliException(ResultCodeEnum.VIDEO_DELETE_ALIYUN_ERROR);
        }
    }
}